﻿using CMS.Models;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class OutputCargoViewModel :ViewModelBase
    {
        private double sum = 0;
        /// <summary>
        /// 统计库存数量
        /// </summary>
        public double Sum

        {
            get { return sum; }
            set { sum = value; RaisePropertyChanged(nameof(Sum)); }
        }
        public AppData AppData { get; set; } = AppData.Instance;
        /// <summary>
        /// 当前物资
        /// </summary>
        public Cardgo Cargo { get; set; }
        private Record record = new Record();
        /// <summary>
        /// 出库流水单
        /// </summary>
        public Record Record

        {
            get { return record; }
            set { record = value; RaisePropertyChanged(nameof(Record)); }
        }
        /// <summary>
        /// 关闭窗体
        /// </summary>
        public RelayCommand<Window> AddOutputCloseCommand
        {
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                    if (arg == null) return;
                    arg.Close();
                });
            }
        }
        /// <summary>
        /// 添加新流水出库
        /// </summary>
        public RelayCommand<Window> AddRecordOutputCommand
        {
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                    if (this.Sum < record.Number)
                    {
                        MessageBox.Show("出库量应小于或等于库存量","温馨提示");
                        return;
                    }
                    if (Cargo == null) return;
                    record.CargoId = Cargo.Id;
                    record.CargoName = Cargo.Name;
                    if (record.Number == 0) return;
                    record.InserDate = DateTime.Now;
                    record.MenberId = AppData.CurrentUser.Id;
                    record.MenberName = AppData.CurrentUser.Name;
                    record.RecoredType = false;//表示出库
                    var count = new RecordProvider().Insert(record);
                    if (count == 0)
                    {
                        MessageBox.Show("添加失败");
                    }
                    else
                    {
                        arg.Close();
                    }
                });
            }
        }
    }
}
